/**
 * Created by Administrator on 2018/4/26/026.
 */
var MMainlayer = cc.Layer.extend({
        upLock:null,
        isUpUnLock:false,
        actionDuration:1,//时间参考

        ctor:function(){
            cc.log("=======MMainlayer ctor=======");
            this._super();
            //加载【配置】
            this.loadConfig();
            //加载【菜单】 天天向上 开始游戏
            this.loadMenu();
            //加载【设置】
            this.loadSet();
            //加载【帮助】
            this.loadHelp();
            //加载【怪物】 底部1号怪和3号怪
            this.loadBackMonster();
            //加载【云朵】地底遮挡在1 5 6 号怪之间的云朵
            this.loadBackSmoke();
            // 加载5号怪
            this.loadForeMonster();
            //加载【云朵】前面遮罩在5号怪上
            this.loadForeSmoke();
            //加载【萝卜】
            this.loadCarrot();
            //加载【前景】
            this.loadForeground();
            //注册事件
            this.registerEvent();
            //画线
            this.testline();
        },

        //注册事件
        registerEvent:function () {
            var listener = cc.EventListener.create({
                event:cc.EventListener.CUSTOM,
                target:this,
                eventName:jf.EventName.UNLOCK_UP,
                callback:this.onUnLockUp,
            });
            cc.eventManager.addListener(listener,this);
        },

        //加载【配置】
        loadConfig:function(){
            cc.log("=======MMainlayer loadConfig=======");
            //是否已经解锁天天向上
            this.isUpUnLock = cc.sys.localStorage.getItem(Config.IS_UP_UNLOCK_KEY) || "NO";
        },

        //加载【菜单】 天天向上 开始游戏
        loadMenu:function(){
            cc.log("=======MMainlayer loadMenu=======");
            //开始游戏
            var startNormal = new cc.Sprite(res.mm_front_btn_start_normal_png);
            var startPress = new cc.Sprite(res.mm_front_btn_start_pressed_png);
            var startDisable = new cc.Sprite(res.mm_front_btn_start_normal_png);
            var start = new cc.MenuItemSprite(
                startNormal,
                startPress,
                startDisable,
                function(){
                    cc.audioEngine.playEffect(res.sd_mm_Select_mp3);
                    cc.log("点击【开始冒险】按钮");
                    cc.director.runScene(new ChooseLevelScene());
                }.bind(this));
            start.setPosition(cc.winSize.width/2 - 8,cc.winSize.height/2+75);

            //天天向上
            var floorNormal = new cc.Sprite(res.mm_front_btn_floor_normal_png);
            var floorPress = new cc.Sprite(res.mm_front_btn_floor_pressed_png);
            var floorDisable = new cc.Sprite(res.mm_front_btn_floor_normal_png);
            var floor = new cc.MenuItemSprite(
                floorNormal,
                floorPress,
                floorDisable,
                function(){
                    cc.log("点击天天向上");
                }.bind(this));
            floor.setPosition(cc.winSize.width/2 -8 ,cc.winSize.height/2 - 45);

            if(this.isUpUnLock == "NO"){
                var upLock = new cc.Sprite(res.mm_front_btn_floor_locked_png);
                floor.addChild(upLock);
                this.upLock = upLock;
                upLock.setPosition(cc.p(floor.width + 5,floor.height/2+25));
            }

            var menu = new cc.Menu(start,floor);
            this.addChild(menu);
            menu.setPosition(cc.p(0,0));
        },

        //加载【设置】
        loadSet:function(){
            var setBg = new cc.Sprite(res.mm_front_monster_4_png);
            this.addChild(setBg);
            setBg.setPosition(cc.winSize.width/2 - 350,490);

            //上下移动
            var moveBy1 = cc.moveBy(this.actionDuration,cc.p(0,-10));
            var moveBy2 = cc.moveBy(this.actionDuration,cc.p(0,10));
            var seq = cc.sequence(moveBy1,moveBy2);
            var actoin = seq.repeatForever();
            setBg.runAction(actoin);

            var set = new cc.Sprite(res.mm_front_btn_setting_normal_png);
            setBg.addChild(set);
            set.setPosition(157,80);
        },

        //加载【帮助】
        loadHelp:function(){
            var helpBg = new cc.Sprite(res.mm_front_monster_6_hand_png);
            this.addChild(helpBg);
            helpBg.setPosition(cc.winSize.width/2 + 270,270);

            var rotateBy1 = cc.rotateBy(this.actionDuration*0.8,-5);
            var rotateBy2 = cc.rotateBy(this.actionDuration*0.8,5);
            var seq = cc.sequence(rotateBy1,rotateBy2);
            var action = seq.repeatForever();
            helpBg.runAction(action);

            var help = new cc.Sprite(res.mm_front_btn_help_normal_png );
            helpBg.addChild(help);
            help.setPosition(cc.p());
            help.setPosition(cc.p(155,365));

            var helpbody = new cc.Sprite(res.mm_front_monster_6_png);
            this.addChild(helpbody);
            helpbody.setPosition(cc.p(cc.winSize.width/2 +400,280));

            var helpbodyMoveby1 = cc.moveBy(this.actionDuration*2,cc.p(0,5));
            var helpbodyMoveby2 = cc.moveBy(this.actionDuration*2,cc.p(0,-5));
            var helpbodyseq = cc.sequence(helpbodyMoveby1,helpbodyMoveby2);
            var helpbodyAction = helpbodyseq.repeatForever();
            helpbody.runAction(helpbodyAction);
        },

        //加载【怪物】底部的1号和3号
        loadBackMonster:function(){
            var leftYellow = new cc.Sprite(res.mm_front_monster_3_png);
            this.addChild(leftYellow);
            leftYellow.setPosition(cc.p(cc.winSize.width/2 -360,220));

            //上下移动
            var yellowMoveBy1 = cc.moveBy(this.actionDuration*0.8,cc.p(0,5));
            var yellowMoveBy2 = cc.moveBy(this.actionDuration*0.8,cc.p(0,-5));
            var yellowSeq = cc.sequence(yellowMoveBy1,yellowMoveBy2);
            var yellowAction = yellowSeq.repeatForever();
            leftYellow.runAction(yellowAction);

            var leftGreen = new cc.Sprite(res.mm_front_monster_1_png);
            this.addChild(leftGreen);
            leftGreen.setPosition(cc.winSize.width/2 -300,220);

            //左右移动
            var greenMoveBy1 = cc.moveBy(this.actionDuration*0.7,cc.p(-3,0));
            var greenMoveBy2 = cc.moveBy(this.actionDuration*0.7,cc.p(3,0));
            var greenseq = cc.sequence(greenMoveBy1,greenMoveBy2);
            var greenAction = greenseq.repeatForever();
            leftGreen.runAction(greenAction);
        },

        //加载【云朵】地底遮挡在1 5 6 号怪之间的云朵
        loadBackSmoke:function () {
            var left = new cc.Sprite(res.mm_front_smoke_1_png);
            this.addChild(left);
            left.setPosition(cc.p(cc.winSize.width/2-410,188));

            var right = new cc.Sprite(res.mm_front_smoke_3_png);
            this.addChild(right);
            right.setPosition(cc.p(cc.winSize.width/2 +405,190));
        },

        // 加载5号怪
        loadForeMonster:function(){
            var rightYellow = new cc.Sprite(res.mm_front_monster_5_png);
            this.addChild(rightYellow);
            rightYellow.setPosition(cc.p(cc.winSize.width/2 +290,185));

            var yellowMoveBy1 = cc.moveBy(this.actionDuration*0.85,cc.p(-3,0));
            var yellowMoveBy2 = cc.moveBy(this.actionDuration*0.85,cc.p(3,0));
            var yellowSeq = cc.sequence(yellowMoveBy1,yellowMoveBy2);
            var yellowAction = yellowSeq.repeatForever();
            rightYellow.runAction(yellowAction);

            var leftCambirdgeBlue = new cc.Sprite(res.mm_front_monster_2_png);
            this.addChild(leftCambirdgeBlue);
            leftCambirdgeBlue.setPosition(cc.p(cc.winSize.width/2 -300,150));

            var action0 = cc.moveTo(this.actionDuration*0.2,cc.p(cc.winSize.width/2-220,170));
            var actiion1 = cc.sequence(action0, cc.callFunc(function(){
                    var blueMove1 = cc.moveBy(this.actionDuration*0.55,cc.p(0,-5));
                    var blueMove2 = cc.moveBy(this.actionDuration*0.55,cc.p(0,5));
                    var blueSeq = cc.sequence(blueMove1, blueMove2);
                    var blueAction = blueSeq.repeatForever();
                    leftCambirdgeBlue.runAction(blueAction);
                },this)
            )
            leftCambirdgeBlue.runAction(actiion1);
        },

        //加载【云朵】前面遮罩在5号怪上
        loadForeSmoke:function(){
            var node = new cc.Sprite(res.mm_front_smoke_2_png);
            this.addChild(node);
            node.setPosition(cc.p(cc.winSize.width/2+320,150));
        },

        //加载【萝卜】
        loadCarrot:function() {
            var node = new cc.Sprite(res.mm_front_carrot_png);
            this.addChild(node);

            //萝卜 贝赛尔曲线缩放运动
            node.setScale(0.7);
            node.setPosition(cc.p(cc.winSize.width / 2 + 320, 120));
            var controlPosition = [
                cc.p(cc.winSize.width / 2 + 400, 100),
                cc.p(cc.winSize.width / 2 + 120, 0),
                cc.p(cc.winSize.width / 2 + 120, 20)];
            var bezierTo = cc.bezierTo(this.actionDuration*0.8,controlPosition);
            var scaleTo = cc.scaleTo(this.actionDuration*0.8,1);
            var spawn =cc.spawn(bezierTo,scaleTo);
            node.runAction(spawn);
        },

        //加载【前景】
        loadForeground:function() {
            var node = new cc.Sprite(res.mm_front_front_png);
            this.addChild(node);
            node.setPosition(cc.p(cc.winSize.width/2,cc.winSize.height/2));
        },

        //收到解锁消息
        onUnLockUp:function (event) {
            var target = event.getCurrentTarget();
            var data = event.getUserData();
            if(data.isSuccess !== _undefined && data.isSuccess){
                //解锁成功
                cc.sys.localStorage.setItem(Config.IS_UP_UNLOCK_KEY,"YES");
                target.isUpUnLock = true;
                target.upLock.removeFromParent();
            }
        },

        //测试画线
        testline:function () {
            // var draw1 = new cc.DrawNode();
            // this.addChild(draw1, 10);
            // var pos = [cc.p(69,127),cc.p(69,500),cc.p(900,500)];
            // draw1.drawSegment(pos[0],pos[1], 1, cc.color(255, 255, 255, 255));
            // var draw2 = new cc.DrawNode();
            // this.addChild(draw2, 10);
            // draw2.drawSegment(pos[1],pos[2], 1, cc.color(255, 255, 255, 255));
            // var draw3 = new cc.DrawNode();
            // this.addChild(draw3, 10);
            // draw3.drawSegment(pos[0],pos[2], 1, cc.color(255, 255, 255, 255));
            // // draw1.setVisible(false);
            //
            //
            // var delta1 = cc.pSub(pos[2], pos[0]);
            // var label1 = new cc.LabelTTF("delta1","Arial",20);
            // this.addChild(label1);
            // cc.log("======pos===="+JSON.stringify(delta1));
            // label1.setPosition(cc.p(delta1.x/2+pos[0].x,delta1.y/2+pos[0].y +10));
            // label1.setColor(cc.p(255,0,0));
            // var deltaRotation = -Math.atan2(pos[2].y-pos[0].y,pos[2].x-pos[0].x)*180/Math.PI;
            // label1.setRotation(deltaRotation);
            //
            // var delta2 = cc.pSub(pos[1], pos[0]);
            // var label2 = new cc.LabelTTF("delta2","Arial",20);
            // this.addChild(label2);
            // cc.log("======pos===="+JSON.stringify(delta2));
            // label2.setPosition(cc.p(delta2.x/2+pos[0].x-10,delta2.y/2+pos[0].y));
            // label2.setColor(cc.p(255,0,0));
            // var deltaRotation2 = -Math.atan2(pos[1].y-pos[0].y,pos[1].x-pos[0].x)*180/Math.PI;
            // label2.setRotation(deltaRotation2);
            //
            // var delta3 = cc.pSub(pos[2], pos[1]);
            // var label3 = new cc.LabelTTF("label3","Arial",20);
            // this.addChild(label3);
            // cc.log("======pos===="+JSON.stringify(delta3));
            // label3.setPosition(cc.p(delta3.x/2+pos[1].x,delta3.y/2+pos[1].y+10));
            // label3.setColor(cc.p(255,0,0));
            // var deltaRotation3 = -Math.atan2(pos[2].y-pos[1].y,pos[2].x-pos[1].x)*180/Math.PI;
            // label3.setRotation(deltaRotation3);


            //五人情况
            var draw1 = new cc.DrawNode();
            this.addChild(draw1, 10);
            var pos = [cc.p(69,127),cc.p(69,350),cc.p(250,500),cc.p(700,500),cc.p(900,350)];
            draw1.drawSegment(pos[0],pos[1], 1, cc.color(255, 255, 255, 255));
            var draw2 = new cc.DrawNode();
            this.addChild(draw2, 10);
            draw2.drawSegment(pos[0],pos[2], 1, cc.color(255, 255, 255, 255));
            var draw3 = new cc.DrawNode();
            this.addChild(draw3, 10);
            draw3.drawSegment(pos[0],pos[3], 1, cc.color(255, 255, 255, 255));
            var draw4 = new cc.DrawNode();
            this.addChild(draw4, 10);
            draw4.drawSegment(pos[0],pos[4], 1, cc.color(255, 255, 255, 255));
            var draw12 = new cc.DrawNode();
            this.addChild(draw12, 10);
            draw12.drawSegment(pos[1],pos[2], 1, cc.color(255, 255, 255, 255));
            var draw13 = new cc.DrawNode();
            this.addChild(draw13, 10);
            draw13.drawSegment(pos[1],pos[3], 1, cc.color(255, 255, 255, 255));
            var draw14 = new cc.DrawNode();
            this.addChild(draw14, 10);
            draw14.drawSegment(pos[1],pos[4], 1, cc.color(255, 255, 255, 255));
            var draw23 = new cc.DrawNode();
            this.addChild(draw23, 10);
            draw23.drawSegment(pos[2],pos[3], 1, cc.color(255, 255, 255, 255));
            var draw24 = new cc.DrawNode();
            this.addChild(draw24, 10);
            draw24.drawSegment(pos[2],pos[4], 1, cc.color(255, 255, 255, 255));
            var draw34 = new cc.DrawNode();
            this.addChild(draw34, 10);
            draw34.drawSegment(pos[3],pos[4], 1, cc.color(255, 255, 255, 255));

            var delta01 = cc.pSub(pos[1], pos[0]);
            var label01 = new cc.LabelTTF("delta1","Arial",20);
            this.addChild(label01);
            cc.log("======pos===="+JSON.stringify(delta01));
            label01.setPosition(cc.p(delta01.x/2+pos[0].x,delta01.y/2+pos[0].y +10));
            label01.setColor(cc.p(255,0,0));
            var deltaRotation = -Math.atan2(pos[1].y-pos[0].y,pos[1].x-pos[0].x)*180/Math.PI;
            label01.setRotation(deltaRotation);

            var delta02 = cc.pSub(pos[2], pos[0]);
            var label02 = new cc.LabelTTF("delta02","Arial",20);
            this.addChild(label02);
            cc.log("======pos===="+JSON.stringify(delta02));
            label02.setPosition(cc.p(delta02.x/2+pos[0].x,delta02.y/2+pos[0].y +10));
            label02.setColor(cc.p(255,0,0));
            var deltaRotation02 = -Math.atan2(pos[2].y-pos[0].y,pos[2].x-pos[0].x)*180/Math.PI;
            label02.setRotation(deltaRotation02);

            var delta03 = cc.pSub(pos[3], pos[0]);
            var label03 = new cc.LabelTTF("delta03","Arial",20);
            this.addChild(label03);
            cc.log("======pos===="+JSON.stringify(delta03));
            label03.setPosition(cc.p(delta03.x/2+pos[0].x,delta03.y/2+pos[0].y +10));
            label03.setColor(cc.p(255,0,0));
            var deltaRotation03 = -Math.atan2(pos[3].y-pos[0].y,pos[3].x-pos[0].x)*180/Math.PI;
            label03.setRotation(deltaRotation03);

            var delta04 = cc.pSub(pos[4], pos[0]);
            var label04 = new cc.LabelTTF("label04","Arial",20);
            this.addChild(label04);
            cc.log("======pos===="+JSON.stringify(delta03));
            label04.setPosition(cc.p(delta04.x/2+pos[0].x,delta04.y/2+pos[0].y +10));
            label04.setColor(cc.p(255,0,0));
            var deltaRotation04 = -Math.atan2(pos[4].y-pos[0].y,pos[4].x-pos[0].x)*180/Math.PI;
            label04.setRotation(deltaRotation04);

            var delta12 = cc.pSub(pos[2], pos[1]);
            var label12 = new cc.LabelTTF("delta12","Arial",20);
            this.addChild(label12);
            cc.log("======pos===="+JSON.stringify(delta03));
            label12.setPosition(cc.p(delta12.x/2+pos[1].x,delta12.y/2+pos[1].y +10));
            label12.setColor(cc.p(255,0,0));
            var deltaRotation12 = -Math.atan2(pos[2].y-pos[1].y,pos[2].x-pos[1].x)*180/Math.PI;
            label12.setRotation(deltaRotation12);

            var delta13 = cc.pSub(pos[3], pos[1]);
            var label13 = new cc.LabelTTF("delta13","Arial",20);
            this.addChild(label13);
            cc.log("======pos===="+JSON.stringify(delta03));
            label13.setPosition(cc.p(delta13.x/2+pos[1].x,delta13.y/2+pos[1].y +10));
            label13.setColor(cc.p(255,0,0));
            var deltaRotation13 = -Math.atan2(pos[3].y-pos[1].y,pos[3].x-pos[1].x)*180/Math.PI;
            label13.setRotation(deltaRotation13);

            var delta14 = cc.pSub(pos[4], pos[1]);
            var label14 = new cc.LabelTTF("delta14","Arial",20);
            this.addChild(label14);
            cc.log("======pos===="+JSON.stringify(delta14));
            label14.setPosition(cc.p(delta14.x/2+pos[1].x,delta14.y/2+pos[1].y +10));
            label14.setColor(cc.p(255,0,0));
            var deltaRotation14 = -Math.atan2(pos[4].y-pos[1].y,pos[4].x-pos[1].x)*180/Math.PI;
            label14.setRotation(deltaRotation14);

            var delta13 = cc.pSub(pos[3], pos[1]);
            var label13 = new cc.LabelTTF("delta13","Arial",20);
            this.addChild(label13);
            cc.log("======pos===="+JSON.stringify(delta03));
            label13.setPosition(cc.p(delta13.x/2+pos[1].x,delta13.y/2+pos[1].y +10));
            label13.setColor(cc.p(255,0,0));
            var deltaRotation13 = -Math.atan2(pos[3].y-pos[1].y,pos[3].x-pos[1].x)*180/Math.PI;
            label13.setRotation(deltaRotation13);

            var delta14 = cc.pSub(pos[4], pos[1]);
            var label14 = new cc.LabelTTF("delta14","Arial",20);
            this.addChild(label14);
            cc.log("======pos===="+JSON.stringify(delta14));
            label14.setPosition(cc.p(delta14.x/2+pos[1].x,delta14.y/2+pos[1].y +10));
            label14.setColor(cc.p(255,0,0));
            var deltaRotation14 = -Math.atan2(pos[4].y-pos[1].y,pos[4].x-pos[1].x)*180/Math.PI;
            label14.setRotation(deltaRotation14);

            var delta13 = cc.pSub(pos[3], pos[1]);
            var label13 = new cc.LabelTTF("delta13","Arial",20);
            this.addChild(label13);
            cc.log("======pos===="+JSON.stringify(delta03));
            label13.setPosition(cc.p(delta13.x/2+pos[1].x,delta13.y/2+pos[1].y +10));
            label13.setColor(cc.p(255,0,0));
            var deltaRotation13 = -Math.atan2(pos[3].y-pos[1].y,pos[3].x-pos[1].x)*180/Math.PI;
            label13.setRotation(deltaRotation13);

            var delta23 = cc.pSub(pos[3], pos[2]);
            var label23 = new cc.LabelTTF("delta23","Arial",20);
            this.addChild(label23);
            cc.log("======pos===="+JSON.stringify(delta23));
            label23.setPosition(cc.p(delta23.x/2+pos[2].x,delta23.y/2+pos[2].y +10));
            label23.setColor(cc.p(255,0,0));
            var deltaRotation23 = -Math.atan2(pos[2].y-pos[2].y,pos[3].x-pos[2].x)*180/Math.PI;
            label23.setRotation(deltaRotation23);

            var delta24 = cc.pSub(pos[4], pos[2]);
            var label24 = new cc.LabelTTF("delta24","Arial",20);
            this.addChild(label24);
            cc.log("======pos===="+JSON.stringify(delta24));
            label24.setPosition(cc.p(delta24.x/2+pos[2].x,delta24.y/2+pos[2].y +10));
            label24.setColor(cc.p(255,0,0));
            var deltaRotation24 = -Math.atan2(pos[4].y-pos[2].y,pos[4].x-pos[2].x)*180/Math.PI;
            label24.setRotation(deltaRotation24);

            var delta34 = cc.pSub(pos[4], pos[3]);
            var label34 = new cc.LabelTTF("delta34","Arial",20);
            this.addChild(label34);
            cc.log("======pos===="+JSON.stringify(delta34));
            label34.setPosition(cc.p(delta34.x/2+pos[3].x,delta34.y/2+pos[3].y +10));
            label34.setColor(cc.p(255,0,0));
            var deltaRotation34 = -Math.atan2(pos[4].y-pos[3].y,pos[4].x-pos[3].x)*180/Math.PI;
            label34.setRotation(deltaRotation34);
        },

    });